<template>
  <div class="wrap">
    <el-button
      @click="drawer = true"
      type="primary"
      style="margin-left: 16px"
      class="btns"
    >
      添加
    </el-button>
    <el-table :data="list" style="width: 100%">
      <el-table-column type="index" label="序号" width="80"></el-table-column>
      <el-table-column
        class-name="pro-name"
        prop="goods_name"
        label="名称"
        width="350"
      ></el-table-column>
      <el-table-column sortable prop="goods_price" label="原价" width="180">
        <!-- sortable是el-table-column中的排序小属性 -->
      </el-table-column>
      <el-table-column sortable prop="goods_id" label="ID" width="180">
      </el-table-column>
      <el-table-column
        prop="goods_number"
        label="数量"
        width="180"
      ></el-table-column>
      <el-table-column label="操作" width="180">
        <template v-slot:default="xxx">
          <el-button @click="delPro(xxx.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-drawer
      title="我是标题"
      :visible.sync="drawer"
      :direction="direction"
      :before-close="handleClose"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="商品名称" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="分类列表" prop="list">
          <el-input v-model="ruleForm.list" disabled=""></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="price">
          <el-input
            v-model="ruleForm.price"
            type="number"
            placeholder="请输入数字"
          ></el-input>
        </el-form-item>
        <el-form-item label="商品数量" prop="number">
          <el-input
            v-model="ruleForm.number"
            type="number"
            placeholder="请输入数字"
          ></el-input>
        </el-form-item>
        <el-form-item label="商品重量" prop="weight">
          <el-input
            v-model="ruleForm.weight"
            type="number"
            placeholder="请输入数字"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >立即添加</el-button
          >
          <el-button @click="resetForm()">重置信息</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>
<script>
import {
  AddProductionList,
  RomoveProductionList,
  getProductionList,
} from "@/api/production.js";
import { requestList } from "@/utils/index.js";
export default {
  data() {
    return {
      list: [],
      total: 0,
      pageSize: 10,
      page: 1,
      drawer: false,
      direction: "rtl",
      ruleForm: {
        name: "",
        list: "123",
        price: "",
        number: "",
        weight: "",
      },
      rules: {
        name: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          { min: 1, message: "长度不得少于一个字", trigger: "blur" },
        ],
      },
    };
  },

  methods: {
    //删除商品数据
    delPro(proItem) {
      // console.log(proItem);
      RomoveProductionList(proItem.goods_id).then((res) => {
        // console.log(res);
        this.requestList();
      });
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    submitForm(ruleForm) {
      AddProductionList({
        goods_name: this.ruleForm.name,
        goods_cat: this.ruleForm.list,
        goods_price: this.ruleForm.price,
        goods_number: this.ruleForm.number,
        goods_weight: this.ruleForm.weight,
      }).then((res) => {
        // console.log(res);
        if (res.data.meta.status === 200) {
          // console.log(res);
          this.list = res.data.data.goods;
          // 更新总数
          this.total = res.data.data.total;
        }
      });
    },
    resetForm() {
      this.ruleForm.name = "";
      // this.ruleForm.list = "";
      this.ruleForm.price = "";
      this.ruleForm.number = "";
      this.ruleForm.weight = "";
    },
    requestList() {
      getProductionList({
        pagenum: this.page,
        pagesize: this.pageSize,
      }).then((res) => {
        // console.log(res);
        if (res.data.meta.status === 200) {
          // console.log(res);
          this.list = res.data.data.goods;
          // 更新总数
          this.total = res.data.data.total;
        }
      });
    },
  },
  mounted() {
    this.requestList();
  },
};
</script>

<style scoped lang="scss">
.wrap{
  margin-top: 10px;
}
.btns {
  float: left;
  width: 100px;
  margin-bottom: 20px;
}
::v-deep(.pro-name .cell) {
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}
el-pagination {
  display: block;
}
</style>
